package com.sl.au.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sl.au.entity.PayPlan;
import com.sl.au.entity.PayRegist;
import com.sl.au.entity.PaymentInitial;
import com.sl.au.entity.PaymentPlan;
import com.sl.au.entity.PaymentRegist;
import com.sl.au.repository.PayPlanRepository;
import com.sl.au.repository.PayRegistRepository;
import com.sl.au.repository.PaymentInitialRepository;
import com.sl.au.repository.PaymentPlanRepository;
import com.sl.au.repository.PaymentRegistRepository;
import com.sl.au.service.PaymentInitialService;


@Service("PaymentInitialService")
@Transactional
public class PaymentInitalServiceImpl extends BaseServiceImpl implements PaymentInitialService {
	@Autowired
	PaymentInitialRepository paymentInitialRepository;
	@Autowired
	PayRegistRepository payRegistRepository;
	@Autowired
	PaymentPlanRepository paymentPlanRepository;
	@Autowired
	PayPlanRepository payPlanRepository;
	@Autowired
	PaymentRegistRepository paymentRegistRepository;

	public PaymentInitialRepository getRepository() {

		return this.paymentInitialRepository;
	}

	@Override
	public PayRegistRepository getPayRegistRepository() {
		return this.payRegistRepository;
	}
	@Override
	public PayPlanRepository getPayPlanRepository() {
		return this.payPlanRepository;
	}

	@Override
	public PaymentPlanRepository getPaymentPlanRepository() {
		return this.paymentPlanRepository;
	}

	@Override
	public PaymentRegistRepository getPaymentRegistRepository() {
		return this.paymentRegistRepository;
	}

	/**
	 * 获取合同相关应收账计划
	 * @param contractId
	 * @return
	 */
	@Override
	public List<PaymentInitial> getPaymentSchedule(String contractId,String contractType) {
		List<PaymentInitial> paymentInitials=null;
		paymentInitials=paymentInitialRepository.findByContractIdAndContractType(contractId,contractType);//通过合同id和类型获得该合同的应收账计划
		return paymentInitials;
	}

	/**
	 * 获取合同对应回款历史
	 * @param contractId
	 * @param contractType
	 * @return
	 */
	@Override
	public List<PayRegist> getPayHistory(int start, int length,String contractId, String contractType,String userId,String orderName ,String dir) {
   		Page<PayRegist> payRegists=null;
		Pageable pageable=new PageRequest(start/length,length, Sort.Direction.DESC,"createTime");
		payRegists=payRegistRepository.findByContractIdAndContractType(contractId,contractType,userId,pageable);
		return payRegists.getContent();
	}
	/**
	 * 获取合同对应回款历史数据条数
	 * @param contractId
	 * @return
	 */
	@Override
	public Long getPayRegistCount(String contractId,String userId) {
		Long count=payRegistRepository.getPayRegistCount(contractId,userId);
		return count;
	}
	/**
	 * 获取合同对应开票历史
	 * @param contractId
	 * @param contractType
	 * @return
	 */
	@Override
	public List<PaymentRegist> getPaymentHistory(int start, int length,String contractId, String contractType,String userId,String orderName ,String dir) {
		Page<PaymentRegist> paymentRegists=null;
		Pageable pageable=new PageRequest(start/length,length, Sort.Direction.DESC,"createTime");
		paymentRegists=paymentRegistRepository.findByContractIdAndContractType(contractId,contractType,userId,pageable);
		return paymentRegists.getContent();
	}
	/**
	 * 获取合同对应开票历史数据条数
	 * @param contractId
	 * @return
	 */
	@Override
	public Long getPaymentRegistCount(String contractId,String userId) {
		Long count=paymentRegistRepository.getPaymentRegistCount(contractId,userId);
		return count;
	}
	/**
	 *获取回款-计划表数据
	 * @param contractId
	 * @param contractType
	 * @return
	 */
	@Override
	public List<PayPlan> getPayPlan(int start, int length,String contractId, String contractType,String orderName ,String dir) {
		Page<PayPlan> payPlans=null;
		Pageable pageable=new PageRequest(start/length,length, Sort.Direction.ASC,"id");
		payPlans=payPlanRepository.findByContractIdAndContractType(contractId,contractType,pageable);
		return payPlans.getContent();
	}
	/**
	 *获取回款-计划表数据条数
	 * @param contractId
	 * @return
	 */
	@Override
	public Long getPayPlanCount(String contractId) {
		Long count=payPlanRepository.getPayPlanCount(contractId);
		return count;
	}
	/**
	 *获取开票-计划表数据
	 * @param contractId
	 * @param contractType
	 * @return
	 */
	@Override
	public List<PaymentPlan> getPaymentPlan(int start, int length,String contractId, String contractType,String orderName ,String dir) {
		Page<PaymentPlan> paymentPlans=null;
		Pageable pageable=new PageRequest(start/length,length, Sort.Direction.ASC,"id");
		paymentPlans=paymentPlanRepository.findByContractIdAndContractType(contractId,contractType,pageable);
		return paymentPlans.getContent();
	}
	/**
	 *获取开票-计划表数据条数
	 * @param contractId
	 * @return
	 */
	@Override
	public Long getPaymentPlanCount(String contractId) {
        Long count=paymentPlanRepository.getPaymentPlanCount(contractId);
		return count;
	}

	/**
	 * 新增保存回款
	 * @param payRegist
	 */
	@Override
	public void savePayRegist(PayRegist payRegist) {
		payRegistRepository.save(payRegist);
	}
	/**
	 * 保存开票
	 * @param paymentRegist
	 */
	@Override
	public void savePaymentRegist(PaymentRegist paymentRegist) {
		paymentRegistRepository.save(paymentRegist);
	}

	/**
	 * 保存回款-计划
	 * @param fund
	 * @param scheduleSum
	 * @param paidStage
	 * @param scheduleDate
	 */
	@Override
	public void savePayPlan(String fund,String planId, float scheduleSum, float paidStage, Date scheduleDate,PayRegist payRegist) {
		long interval = 24 * 60 * 60 * 1000;// 一天
		PayPlan payPlan = new PayPlan();
		payPlan.setFund(fund);
		payPlan.setPlanId(planId);
		payPlan.setScheduleSum(scheduleSum);
		payPlan.setPaidStage(paidStage);
		payPlan.setSchedulePayDate(scheduleDate);
		if(scheduleDate != null){
			payPlan.setDeviationDate((payRegist.getPaidDate().getTime() - scheduleDate.getTime())/interval);
		}
		payPlan.setMemo(payRegist.getMemo());
		payPlan.setContractId(payRegist.getContractId());
		payPlan.setContractType(payRegist.getContractType());
		payPlan.setPayHistoryId(payRegist.getId());
		payPlanRepository.save(payPlan);
		String inParam = payRegist.getContractId();
		//向存储过程传递参数不返回值
		//payPlanRepository.payNotionalPooling(inParam);
	}
	private boolean isNull(Date scheduleDate) {
		// TODO Auto-generated method stub
		return false;
	}

	/**
	 * 保存开票-计划
	 * @param fund
	 * @param scheduleSum
	 * @param paymentStage
	 * @param scheduleDate
	 */
	@Override
	public void savePaymentPlan(String fund,String planId, float scheduleSum, float paymentStage, Date scheduleDate, PaymentRegist paymentRegist) {
		PaymentPlan paymentPlan = new PaymentPlan();
		paymentPlan.setFund(fund);
		paymentPlan.setPlanId(planId);
		paymentPlan.setScheduleSum(scheduleSum);
		paymentPlan.setPaymentStage(paymentStage);
		paymentPlan.setSchedulePayDate(scheduleDate);
		paymentPlan.setMemo(paymentRegist.getMemo());
		paymentPlan.setContractId(paymentRegist.getContractId());
		paymentPlan.setContractType(paymentRegist.getContractType());
		paymentPlan.setPaymentHistoryId(paymentRegist.getId());
		paymentPlanRepository.save(paymentPlan);
	}

	/**
	 * 初始化计划
	 * @param contractId
	 * @param contractType
	 */
	@Override
	public void getPaySchedule(String contractId, String contractType) {
		List<PaymentInitial> paymentInitialList = null;
		paymentInitialList = paymentInitialRepository.findByContractId(contractId);
		String fund[]={"预付款","进度款","发货款","验收款","调试款","质保金","运费"};//7个款项按顺序保存
		if (paymentInitialList.isEmpty()) {//如果合同没有初始化计划就自动生成计划
			for (int i = 0; i <= 6; i++) {
				PaymentInitial paymentInitial =new PaymentInitial();
					paymentInitial.setFund(fund[i]);
					paymentInitial.setAmount(0);
					//paymentInitial.setPayDate(new Date());
					paymentInitial.setMemo("");
					paymentInitial.setContractId(contractId);
					paymentInitial.setContractType(contractType);
					paymentInitialRepository.save(paymentInitial);
				PayPlan payPlan=new PayPlan();
				payPlan.setFund(fund[i]);
				payPlan.setScheduleSum(0);
				payPlan.setContractId(contractId);
				payPlan.setContractType(contractId);
				//payPlan.setSchedulePayDate(new Date());
				payPlanRepository.save(payPlan);
				PaymentPlan paymentPlan=new PaymentPlan();
				paymentPlan.setFund(fund[i]);
				//paymentPlan.setSchedulePayDate(new Date());
				paymentPlan.setScheduleSum(0);
				paymentPlan.setContractId(contractId);
				paymentPlan.setContractType(contractId);
				paymentPlanRepository.save(paymentPlan);
				try {
					Thread.sleep(5);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
	/**
	 * 保存未抵消回款金额的款项
	 * @param contractId
	 */
	@Override
	public void saveNotPayPlan(List<PayPlan> newPayPlan1,String[] fund, String contractId,Date[] scheduleDate,float[] scheduleSum) {
		PayPlan newData = newPayPlan1.get(0);//获得上一次回款抵消到哪个款项
		int num = 0;
		for (int n = 0; n <= 6; n++) {//获得上一次抵消款项的序号（及第几个款项）
			if (fund[n].equals(newData.getFund())) {
				num = n;
			}
		}
		if(newData.getPaidStage()==newData.getScheduleSum()) {//如果上一次的回款已经抵消完了一个款项的金额
			for (int i = num + 1; i <= 6; i++) {//就从下一个款项开始保存没有抵消的款项
				PayPlan payPlan = new PayPlan();
				payPlan.setFund(fund[i]);
				payPlan.setSchedulePayDate(scheduleDate[i]);
				payPlan.setScheduleSum(scheduleSum[i]);
				payPlan.setContractId(contractId);
				payPlan.setContractType(contractId);
				payPlanRepository.save(payPlan);
				try {
					Thread.sleep(5);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
		else {//上次的回款没有抵消完，就要保存上一次还未抵消完的款项及剩余金额
			PayPlan payPlan=new PayPlan();
			payPlan.setFund(fund[num]);
			payPlan.setSchedulePayDate(scheduleDate[num]);
			payPlan.setScheduleSum(newData.getScheduleSum() - newData.getPaidStage());
			payPlan.setContractId(contractId);
			payPlan.setContractType(contractId);
			payPlanRepository.save(payPlan);
				for(int i=num+1;i<=6;i++){
					PayPlan payPlan1=new PayPlan();
					payPlan1.setFund(fund[i]);
					payPlan1.setSchedulePayDate(scheduleDate[i]);
					payPlan1.setScheduleSum(scheduleSum[i]);
					payPlan1.setContractId(contractId);
					payPlan1.setContractType(contractId);
					payPlanRepository.save(payPlan1);
					try {
						Thread.sleep(5);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
	    }
	/**
	 * 获取当前回款阶段信息
	 * @param newPayPlan
	 */
	@Override
	public List<Object> getNewPayPlan(List<PayPlan> newPayPlan) {
		List<Object> list=null;
		PayPlan newData = newPayPlan.get(0);
		String fundNew = newData.getFund();
		float scheduleSumNew = newData.getScheduleSum();
		float paidStage = newData.getPaidStage();
		int num = 0;
		String fund[]={"预付款","进度款","发货款","验收款","调试款","质保金","运费"};
		for (int n = 0; n <= 6; n++) {
			if (fund[n].equals(fundNew)) {
				num = n;
			}
		}
		list.add(num);
		list.add(paidStage);
		list.add(scheduleSumNew);
		return list;
	}
	/**
	 * 保存未抵消开票金额的款项（原理同回款）
	 * @param contractId
	 */
	@Override
	public void saveNotPaymentPlan(List<PaymentPlan> newPaymentPlan1, String[] fund, String contractId, Date[] scheduleDate, float[] scheduleSum) {
		PaymentPlan newData = newPaymentPlan1.get(0);
		int num = 0;
		for (int n = 0; n <= 6; n++) {
			if (fund[n].equals(newData.getFund())) {
				num = n;
			}
		}
		if(newData.getPaymentStage()==newData.getScheduleSum()) {
			for (int i = num + 1; i <= 6; i++) {
				PaymentPlan paymentPlan = new PaymentPlan();
				paymentPlan.setFund(fund[i]);
				paymentPlan.setSchedulePayDate(scheduleDate[i]);
				paymentPlan.setScheduleSum(scheduleSum[i]);
				paymentPlan.setContractId(contractId);
				paymentPlan.setContractType(contractId);
				paymentPlanRepository.save(paymentPlan);
				try {
					Thread.sleep(5);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
		else {
			PaymentPlan paymentPlan=new PaymentPlan();
			paymentPlan.setFund(fund[num]);
			paymentPlan.setSchedulePayDate(scheduleDate[num]);
			paymentPlan.setScheduleSum(newData.getScheduleSum() - newData.getPaymentStage());
			paymentPlan.setContractId(contractId);
			paymentPlan.setContractType(contractId);
			paymentPlanRepository.save(paymentPlan);
			for(int i=num+1;i<=6;i++){
				PaymentPlan paymentPlan1=new PaymentPlan();
				paymentPlan1.setFund(fund[i]);
				paymentPlan1.setSchedulePayDate(scheduleDate[i]);
				paymentPlan1.setScheduleSum(scheduleSum[i]);
				paymentPlan1.setContractId(contractId);
				paymentPlan1.setContractType(contractId);
				paymentPlanRepository.save(paymentPlan1);
				try {
					Thread.sleep(5);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * 生成回款计划对应表
	 * @param payRegist
	 * @return
	 */
	@Override
	public java.util.Map<String, Object> setPayPlan(PayRegist payRegist) {
		java.util.Map<String, Object> map = new HashMap<>();
		List<PaymentInitial> paymentSchedules =paymentInitialRepository.findByContractIdAndContractType(payRegist.getContractId(), payRegist.getContractType());
		String fund[] = new String[7];
		float scheduleSum[] = new float[7];
		Date scheduleDate[] = new Date[7];
		String planId[] = new String[7];
		float sumScheduleSum=0;
		for (int n = 0; n <= 6; n++) {
			fund[n] = paymentSchedules.get(n).getFund();//获得计划款项，按顺序创建时间获取
			scheduleSum[n] = paymentSchedules.get(n).getAmount();
			scheduleDate[n] = paymentSchedules.get(n).getPayDate();
			planId[n] = paymentSchedules.get(n).getId();
			sumScheduleSum=sumScheduleSum+scheduleSum[n];//获得计划总金额，对应每个款项
		}
		if(sumScheduleSum==0){
			map.put("code",0);
		}
		else {
			this.madePayPlan(payRegist,fund,paymentSchedules,scheduleSum,scheduleDate,planId);
			map=this.getPaid(payRegist,fund,scheduleSum,scheduleDate);
		}
		return map;
	}
	/**
	 * 获取回款总额
	 * @return
	 */
	public java.util.Map<String, Object> getPaid(PayRegist payRegist,String fund[],float scheduleSum[],Date scheduleDate[]) {
		java.util.Map<String, Object> map = new HashMap<>();
		List<PayPlan> notPayPlan = null;
		notPayPlan = payPlanRepository.getNewsPayPlan(payRegist.getContractId());//获取回款-计划数据表中未抵消的第一条数据
		if(notPayPlan.isEmpty()){
			List<PayPlan> newPayPlan1 = null;
			newPayPlan1 = payPlanRepository.getNewPayPlan(payRegist.getContractId(), payRegist.getContractType());//获取回款-计划数据表中第一条数据
			this.saveNotPayPlan(newPayPlan1, fund, payRegist.getContractId(), scheduleDate, scheduleSum);//保存未抵消款项
		}
		else {
			PayPlan payPlan=new PayPlan();
			payPlan=notPayPlan.get(notPayPlan.size()-1);
			payPlanRepository.updataChange(payPlan.getContractId(), payPlan.getCreateTime());//删除修改的回款之后的回款-计划记录
			List<PayPlan> newPayPlan1 = null;
			newPayPlan1 = payPlanRepository.getNewPayPlan(payRegist.getContractId(), payRegist.getContractType());
			this.saveNotPayPlan(newPayPlan1, fund, payRegist.getContractId(), scheduleDate, scheduleSum);
		}
		float[] paidSum=payRegistRepository.getPaySum(payRegist.getContractId());
		float sums=0;
		for(int i=0;i<paidSum.length;i++){
			sums=sums+paidSum[i];
		}
		map.put("code",1);
		map.put("sum",sums);
		return map;
	}
	/**
	 * 抵消款项
	 */
	public void madePayPlan(PayRegist payRegist,String fund[],List<PaymentInitial> paymentSchedules,float scheduleSum[],Date scheduleDate[],String planId[]){
		List<PayPlan> newPayPlan = null;
		newPayPlan =payPlanRepository.getNewPayPlan(payRegist.getContractId(), payRegist.getContractType());//获取回款-计划数据表中最后一条记录
		float sum[] = new float[7];
		if (newPayPlan.size() > 0) {//如果该合同没有回款计划记录，既以前没有回过款
			PayPlan newData = newPayPlan.get(0);
			String fundNew = newData.getFund();
			float scheduleSumNew = newData.getScheduleSum();
			float paidStage = newData.getPaidStage();
			int num = 0;
			for (int n = 0; n <= 6; n++) {
				fund[n] = paymentSchedules.get(n).getFund();
				if (fund[n].equals(fundNew)) {
					num = n;
				}
			}
			if (paidStage == scheduleSumNew) {
				sum[num] = payRegist.getPaidSum();
				for (int i = num + 1; i <= 6; ) {
					if (scheduleSum[i] >= sum[i - 1]) {
						this.savePayPlan(fund[i], planId[i], scheduleSum[i], sum[i - 1], scheduleDate[i], payRegist);
						break;
					} else {
						this.savePayPlan(fund[i], planId[i], scheduleSum[i], scheduleSum[i], scheduleDate[i], payRegist);
						sum[i] = sum[i - 1] - scheduleSum[i];
						i++;
						try {
							Thread.sleep(5);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
				}
			} else {
				sum[num] = scheduleSumNew - paidStage;
				if (sum[num] >= payRegist.getPaidSum()) {
					this.savePayPlan(fund[num], planId[num], sum[num], payRegist.getPaidSum(), scheduleDate[num], payRegist);
				} else {
					this.savePayPlan(fund[num], planId[num], sum[num], sum[num], scheduleDate[num], payRegist);
					sum[num + 1] = payRegist.getPaidSum() - sum[num];
					for (int i = num + 1; i <= 6; ) {
						if (scheduleSum[i] >= sum[i]) {
							this.savePayPlan(fund[i], planId[i], scheduleSum[i], sum[i], scheduleDate[i], payRegist);
							break;
						} else {
							this.savePayPlan(fund[i], planId[i], scheduleSum[i], scheduleSum[i], scheduleDate[i], payRegist);
							i++;
							sum[i] = sum[i - 1] - scheduleSum[i - 1];
							try {
								Thread.sleep(5);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						}
					}
				}
			}
		} else {//以前没有回过款，当前保存的回款就从第一个款项开始抵消
			sum[0] = payRegist.getPaidSum();
			for (int i = 0; i <= 6; ) {
				if (scheduleSum[i] >= sum[i]) {//如果第i个款项的计划金额大于等于回款金额
					this.savePayPlan(fund[i], planId[i], scheduleSum[i], sum[i], scheduleDate[i], payRegist);//就保存当前的回款，并返回不继续抵消
					break;
				} else {//如果小于，既可以将第i个款项抵消完，并且可以继续抵消下一个款项，抵消下一个款项时的原则就是重复以上判断
					this.savePayPlan(fund[i], planId[i], scheduleSum[i], scheduleSum[i], scheduleDate[i], payRegist);
					i++;
					sum[i] = sum[i - 1] - scheduleSum[i - 1];//将已经抵消的金额减掉，作为抵消下一个款项的金额，与下一个款项金额进行比较
				}
				try {
					Thread.sleep(5);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
	/**
	 * 更新回款计划对应表
	 * @return
	 */
	@Override
	public java.util.Map<String, Object> updataPayPlan(PayRegist payRegist,PayRegist payRegist1,float paidSum,Date paidDateOld) {
		java.util.Map<String, Object> map = new HashMap<>();
		List<PayPlan> changePayPaln=null;
		changePayPaln=payPlanRepository.getchangePayPlan(payRegist.getId());
		if(payRegist.getPaidSum()==paidSum){
			for(int a=0;a<changePayPaln.size();a++) {
				PayPlan changeData = changePayPaln.get(a);
				changeData.setMemo(payRegist.getMemo());
				changeData.setDeviationDate((payRegist.getPaidDate().getTime() - paidDateOld.getTime())/(24 * 60 * 60 * 1000)+changeData.getDeviationDate());
				payPlanRepository.save(changeData);
			}
			map.put("code",0);
		}
		else {
			PayPlan changeData = changePayPaln.get(0);
			Date changeTime = changeData.getCreateTime();
			map=this.refreshPayPlan(changeTime, payRegist1.getCreateTime(), payRegist.getContractId(), payRegist.getContractType());
		}
		return map;
	}
	/**
	 * 生成开票计划对应表
	 * @return
	 */
	@Override
	public java.util.Map<String, Object> setPaymentPlan(PaymentRegist paymentRegist) {
		java.util.Map<String, Object> map = new HashMap<>();
		List<PaymentPlan> newPaymentPlan=null;
		newPaymentPlan=paymentPlanRepository.getNewPaymentPlan(paymentRegist.getContractId(), paymentRegist.getContractType());
		List<PaymentInitial> paymentSchedules=paymentInitialRepository.findByContractIdAndContractType(paymentRegist.getContractId(), paymentRegist.getContractType());
		String fund[]=new String[7];
		String planId[]=new String[7];
		float scheduleSum[]=new float[7];
		Date scheduleDate[]=new Date[7];
		float sumScheduleSum=0;
		for(int n=0;n<=6;n++){
			fund[n]=paymentSchedules.get(n).getFund();
			planId[n]=paymentSchedules.get(n).getId();
			scheduleSum[n]=paymentSchedules.get(n).getAmount();
			scheduleDate[n]=paymentSchedules.get(n).getPayDate();
			sumScheduleSum=sumScheduleSum+scheduleSum[n];
		}
		if(sumScheduleSum==0){
			map.put("code",0);
		}
		else {
			float sum[] = new float[7];
			if (newPaymentPlan.size() > 0) {
				PaymentPlan newData = newPaymentPlan.get(0);
				String fundNew = newData.getFund();
				float scheduleSumNew = newData.getScheduleSum();
				float paymentStage = newData.getPaymentStage();
				int num = 0;
				for (int n = 0; n <= 6; n++) {
					fund[n] = paymentSchedules.get(n).getFund();
					if (fund[n].equals(fundNew)) {
						num = n;
					}
				}
				if (paymentStage == scheduleSumNew) {
					sum[num] = paymentRegist.getPaymentSum();
					for (int i = num + 1; i <= 6; ) {
						if (scheduleSum[i] >= sum[i - 1]) {
							this.savePaymentPlan(fund[i], planId[i], scheduleSum[i], sum[i - 1], scheduleDate[i], paymentRegist);
							break;
						} else {
							this.savePaymentPlan(fund[i], planId[i], scheduleSum[i], scheduleSum[i], scheduleDate[i], paymentRegist);
							sum[i] = sum[i - 1] - scheduleSum[i];
							i++;
							try {
								Thread.sleep(5);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						}
					}
				} else {
					sum[num] = scheduleSumNew - paymentStage;
					if (sum[num] >= paymentRegist.getPaymentSum()) {
						this.savePaymentPlan(fund[num], planId[num], sum[num], paymentRegist.getPaymentSum(), scheduleDate[num], paymentRegist);
					} else {
						this.savePaymentPlan(fund[num], planId[num], sum[num], sum[num], scheduleDate[num], paymentRegist);
						sum[num + 1] = paymentRegist.getPaymentSum() - sum[num];
						try {
							Thread.sleep(5);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
						for (int i = num + 1; i <= 6; ) {
							if (scheduleSum[i] >= sum[i]) {
								this.savePaymentPlan(fund[i], planId[i], scheduleSum[i], sum[i], scheduleDate[i], paymentRegist);
								break;
							} else {
								this.savePaymentPlan(fund[i], planId[i], scheduleSum[i], scheduleSum[i], scheduleDate[i], paymentRegist);
								i++;
								sum[i] = sum[i - 1] - scheduleSum[i - 1];
								try {
									Thread.sleep(5);
								} catch (InterruptedException e) {
									e.printStackTrace();
								}
							}
						}
					}
				}
			} else {
				sum[0] = paymentRegist.getPaymentSum();
				for (int i = 0; i <= 6; ) {
					if (scheduleSum[i] >= sum[i]) {
						this.savePaymentPlan(fund[i], planId[i], scheduleSum[i], sum[i], scheduleDate[i], paymentRegist);
						break;
					} else {
						this.savePaymentPlan(fund[i], planId[i], scheduleSum[i], scheduleSum[i], scheduleDate[i], paymentRegist);
						i++;
						sum[i] = sum[i - 1] - scheduleSum[i - 1];
					}
					try {
						Thread.sleep(5);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
			map.put("code",1);
			List<PaymentPlan> notPaymentPlan = null;
			notPaymentPlan = paymentPlanRepository.getNewsPaymentPlan(paymentRegist.getContractId());
			if(notPaymentPlan.isEmpty()){
				List<PaymentPlan> newPaymentPlan1 = null;
				newPaymentPlan1 = paymentPlanRepository.getNewPaymentPlan(paymentRegist.getContractId(), paymentRegist.getContractType());
				this.saveNotPaymentPlan(newPaymentPlan1, fund, paymentRegist.getContractId(), scheduleDate, scheduleSum);
			}
			else {
				PaymentPlan paymentPlan=new PaymentPlan();
				paymentPlan=notPaymentPlan.get(notPaymentPlan.size()-1);
				paymentPlanRepository.updataChange(paymentPlan.getContractId(), paymentPlan.getCreateTime());
				List<PaymentPlan> newPaymentPlan1 = null;
				newPaymentPlan1 = paymentPlanRepository.getNewPaymentPlan(paymentRegist.getContractId(), paymentRegist.getContractType());
				this.saveNotPaymentPlan(newPaymentPlan1, fund, paymentRegist.getContractId(), scheduleDate, scheduleSum);
			}
			float[] paymentSum=paymentRegistRepository.getPaymentSum(paymentRegist.getContractId());
			float sums=0;
			for(int i=0;i<paymentSum.length;i++){
				sums=sums+paymentSum[i];
			}
			map.put("sum",sums);
		}
		return map;
	}
	/**
	 * 更新开票计划对应表
	 * @return
	 */
	@Override
	public java.util.Map<String, Object> updataPaymentPlan(PaymentRegist paymentRegist,PaymentRegist paymentRegist1,float paymentSum) {
		java.util.Map<String, Object> map = new HashMap<>();
		List<PaymentPlan> changePaymentPaln=null;
		changePaymentPaln=paymentPlanRepository.getchangePaymentPlan(paymentRegist.getId());
		if(paymentRegist.getPaymentSum()==paymentSum){
			for(int a=0;a<changePaymentPaln.size();a++) {
				PaymentPlan changeData = changePaymentPaln.get(a);
				changeData.setMemo(paymentRegist.getMemo());
				paymentPlanRepository.save(changeData);
			}
			map.put("code",0);
		}
		else {
			PaymentPlan changeData = changePaymentPaln.get(0);
			Date changeTime = changeData.getCreateTime();
			map=this.refreshPaymentPlan(changeTime, paymentRegist1.getCreateTime(), paymentRegist.getContractId(), paymentRegist.getContractType());
		}
		return map;
	}

	/**
	 * 删除回款后更新回款计划对应表
	 * @param firstDate
	 * @param contractId
	 */
	@Override
	public java.util.Map<String, Object> refreshPayPlan(Date firstDate,Date secondDate, String contractId,String contractType) {
		java.util.Map<String, Object> map = new HashMap<>();
		payPlanRepository.deleteChange(contractId, firstDate);//删除修改的回款在回款计划表中的记录和之后的所有记录
		List<PayRegist> payRegistList = null;
		payRegistList = payRegistRepository.getChanges(contractId, secondDate);//获得修改的回款记录之后的所有回款信息
		float[] paySum = payRegistRepository.getPaySum(contractId);//用来得到所有回款金额，前台显示要用，这里抵消款项没关系
		float sums = 0;
		for (int i = 0; i < paySum.length; i++) {
			sums = sums + paySum[i];
		}
		if(sums==0){
			map.put("sum",0);
		}
		else {
		List<PaymentInitial> paymentSchedules = paymentInitialRepository.findByContractIdAndContractType(contractId,contractType);
		String fund[] = new String[7];
		String planId[] = new String[7];
		float scheduleSum[] = new float[7];
		Date scheduleDate[] = new Date[7];
		for (int n = 0; n <= 6; n++){
			fund[n] = paymentSchedules.get(n).getFund();
			planId[n] = paymentSchedules.get(n).getId();
			scheduleSum[n] = paymentSchedules.get(n).getAmount();
			scheduleDate[n] = paymentSchedules.get(n).getPayDate();//得到所有计划金额
		}
			for (int j = 0; j < payRegistList.size(); j++) {
				List<PayPlan> newPayPaln = null;
				newPayPaln = payPlanRepository.getNewPayPlan(contractId, contractType);//获得回款计划表中现在的记录
				float sum[] = new float[7];
				if (newPayPaln.size() > 0) {//回款计划表中还有记录
					PayPlan newData = newPayPaln.get(0);//取最后一条记录
					String fundNew = newData.getFund();//取最后一条记录是哪个款项
					float scheduleSumNew = newData.getScheduleSum();//取最后一条记录是哪个计划金额
					float paidStage = newData.getPaidStage();//取最后一条记录的回款阶段，及抵消了多少金额
					int num = 0;
					for (int n = 0; n <= 6; n++) {
						fund[n] = paymentSchedules.get(n).getFund();
						if (fund[n].equals(fundNew)) {//最后一条记录的款项是第一个款项
							num = n;
						}
					}
					if (paidStage == scheduleSumNew) {//抵消款项部分的原则和新增保存回款的一样
						sum[num] = payRegistList.get(j).getPaidSum();
						for (int i = num + 1; i <= 6; ) {
							if (scheduleSum[i] >= sum[i - 1]) {
								this.savePayPlan(fund[i], planId[i], scheduleSum[i], sum[i - 1], scheduleDate[i], payRegistList.get(j));
								break;
							} else {
								this.savePayPlan(fund[i], planId[i], scheduleSum[i], scheduleSum[i], scheduleDate[i], payRegistList.get(j));
								sum[i] = sum[i - 1] - scheduleSum[i];
								i++;
								try {
									Thread.sleep(5);
								} catch (InterruptedException e) {
									e.printStackTrace();
								}
							}
						}
					} else {
						sum[num] = scheduleSumNew - paidStage;
						if (sum[num] >= payRegistList.get(j).getPaidSum()) {
							this.savePayPlan(fund[num], planId[num], sum[num], payRegistList.get(j).getPaidSum(), scheduleDate[num], payRegistList.get(j));
						} else {
							this.savePayPlan(fund[num], planId[num], sum[num], sum[num], scheduleDate[num], payRegistList.get(j));
							sum[num + 1] = payRegistList.get(j).getPaidSum() - sum[num];
							try {
								Thread.sleep(5);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
							for (int i = num + 1; i <= 6; ) {
								if (scheduleSum[i] >= sum[i]) {
									this.savePayPlan(fund[i], planId[i], scheduleSum[i], sum[i], scheduleDate[i], payRegistList.get(j));
									break;
								} else {
									this.savePayPlan(fund[i], planId[i], scheduleSum[i], scheduleSum[i], scheduleDate[i], payRegistList.get(j));
									i++;
									sum[i] = sum[i - 1] - scheduleSum[i - 1];
									try {
										Thread.sleep(5);
									} catch (InterruptedException e) {
										e.printStackTrace();
									}
								}
							}
						}
					}
				} else {
					sum[0] = payRegistList.get(j).getPaidSum();
					for (int i = 0; i <= 6; ) {
						if (scheduleSum[i] >= sum[i]) {
							this.savePayPlan(fund[i], planId[i], scheduleSum[i], sum[i], scheduleDate[i], payRegistList.get(j));
							break;
						} else {
							this.savePayPlan(fund[i], planId[i], scheduleSum[i], scheduleSum[i], scheduleDate[i], payRegistList.get(j));
							i++;
							sum[i] = sum[i - 1] - scheduleSum[i - 1];
							try {
								Thread.sleep(5);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						}
					}
				}

			}
			List<PayPlan> notPayPlan = null;
			notPayPlan = payPlanRepository.getNewsPayPlan(contractId);
			if (notPayPlan.isEmpty()) {//如果数据库中没有保存为抵消款项，就新增保存为抵消款项
				List<PayPlan> newPayPlan1 = null;
				newPayPlan1 = payPlanRepository.getNewPayPlan(contractId, contractType);
				this.saveNotPayPlan(newPayPlan1, fund, contractId, scheduleDate, scheduleSum);//保存未抵消款项
			} else {//如果已经有了未抵消款项的记录
				PayPlan payPlan = new PayPlan();
				payPlan = notPayPlan.get(notPayPlan.size() - 1);
				payPlanRepository.updataChange(payPlan.getContractId(), payPlan.getCreateTime());//先删除编辑的那条回款之后保存的所有记录
				List<PayPlan> newPayPlan1 = null;
				newPayPlan1 = payPlanRepository.getNewPayPlan(contractId, contractType);
				this.saveNotPayPlan(newPayPlan1, fund, contractId, scheduleDate, scheduleSum);//保存未抵消款项
			}
			map.put("sum", sums);
		}
			map.put("code", 1);
		return map;
	}

	/**
	 * 删除开票后更新开票计划对应表
	 * @param firstDate
	 * @param contractId
	 */
	@Override
	public java.util.Map<String, Object> refreshPaymentPlan(Date firstDate,Date secondDate, String contractId, String contractType) {
		java.util.Map<String, Object> map = new HashMap<>();
		paymentPlanRepository.deleteChange(contractId, firstDate);
		List<PaymentRegist> paymentRegistList = null;
		paymentRegistList = paymentRegistRepository.getChanges(contractId, secondDate);
		float[] paymentSums = paymentRegistRepository.getPaymentSum(contractId);
		float sums = 0;
		for (int i = 0; i < paymentSums.length; i++) {
			sums = sums + paymentSums[i];
		}
		if(sums==0){
			map.put("sum",0);
		}
		else {
			List<PaymentInitial> paymentSchedules = paymentInitialRepository.findByContractIdAndContractType(contractId, contractType);
			String fund[] = new String[7];
			String planId[] = new String[7];
			float scheduleSum[] = new float[7];
			Date scheduleDate[] = new Date[7];
			for (int n = 0; n <= 6; n++) {
				fund[n] = paymentSchedules.get(n).getFund();
				planId[n] = paymentSchedules.get(n).getId();
				scheduleSum[n] = paymentSchedules.get(n).getAmount();
				scheduleDate[n] = paymentSchedules.get(n).getPayDate();
			}
			for (int j = 0; j < paymentRegistList.size(); j++) {
				List<PaymentPlan> newPaymentPaln = null;
				newPaymentPaln = paymentPlanRepository.getNewPaymentPlan(contractId, contractType);
				float sum[] = new float[7];
				if (newPaymentPaln.size() > 0) {
					PaymentPlan newData = newPaymentPaln.get(0);
					String fundNew = newData.getFund();
					float scheduleSumNew = newData.getScheduleSum();
					float paidStage = newData.getPaymentStage();
					int num = 0;
					for (int n = 0; n <= 6; n++) {
						fund[n] = paymentSchedules.get(n).getFund();
						if (fund[n].equals(fundNew)) {
							num = n;
						}
					}
					if (paidStage == scheduleSumNew) {
						sum[num] = paymentRegistList.get(j).getPaymentSum();
						for (int i = num + 1; i <= 6; ) {
							if (scheduleSum[i] >= sum[i - 1]) {
								this.savePaymentPlan(fund[i], planId[i], scheduleSum[i], sum[i - 1], scheduleDate[i], paymentRegistList.get(j));
								break;
							} else {
								this.savePaymentPlan(fund[i], planId[i], scheduleSum[i], scheduleSum[i], scheduleDate[i], paymentRegistList.get(j));
								sum[i] = sum[i - 1] - scheduleSum[i];
								i++;
								try {
									Thread.sleep(5);
								} catch (InterruptedException e) {
									e.printStackTrace();
								}
							}
						}
					} else {
						sum[num] = scheduleSumNew - paidStage;
						if (sum[num] >= paymentRegistList.get(j).getPaymentSum()) {
							this.savePaymentPlan(fund[num], planId[num], sum[num], paymentRegistList.get(j).getPaymentSum(), scheduleDate[num], paymentRegistList.get(j));
						} else {
							this.savePaymentPlan(fund[num], planId[num], sum[num], sum[num], scheduleDate[num], paymentRegistList.get(j));
							sum[num + 1] = paymentRegistList.get(j).getPaymentSum() - sum[num];
							try {
								Thread.sleep(5);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
							for (int i = num + 1; i <= 6; ) {
								if (scheduleSum[i] >= sum[i]) {
									this.savePaymentPlan(fund[i], planId[i], scheduleSum[i], sum[i], scheduleDate[i], paymentRegistList.get(j));
									break;
								} else {
									this.savePaymentPlan(fund[i], planId[i], scheduleSum[i], scheduleSum[i], scheduleDate[i], paymentRegistList.get(j));
									i++;
									sum[i] = sum[i - 1] - scheduleSum[i - 1];
									try {
										Thread.sleep(5);
									} catch (InterruptedException e) {
										e.printStackTrace();
									}
								}
							}
						}
					}
				} else {
					sum[0] = paymentRegistList.get(j).getPaymentSum();
					for (int i = 0; i <= 6; ) {
						if (scheduleSum[i] >= sum[i]) {
							this.savePaymentPlan(fund[i], planId[i], scheduleSum[i], sum[i], scheduleDate[i], paymentRegistList.get(j));
							break;
						} else {
							this.savePaymentPlan(fund[i], planId[i], scheduleSum[i], scheduleSum[i], scheduleDate[i], paymentRegistList.get(j));
							i++;
							sum[i] = sum[i - 1] - scheduleSum[i - 1];
							try {
								Thread.sleep(5);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						}
					}
				}
			}
				List<PaymentPlan> notPaymentPlan = null;
				notPaymentPlan = paymentPlanRepository.getNewsPaymentPlan(contractId);
				if (notPaymentPlan.isEmpty()) {
					List<PaymentPlan> newPaymentPlan1 = null;
					newPaymentPlan1 = paymentPlanRepository.getNewPaymentPlan(contractId, contractType);
					this.saveNotPaymentPlan(newPaymentPlan1, fund, contractId, scheduleDate, scheduleSum);
				} else {
					PaymentPlan paymentPlan = new PaymentPlan();
					paymentPlan = notPaymentPlan.get(notPaymentPlan.size() - 1);
					paymentPlanRepository.updataChange(paymentPlan.getContractId(), paymentPlan.getCreateTime());
					List<PaymentPlan> newPaymentPlan1 = null;
					newPaymentPlan1 = paymentPlanRepository.getNewPaymentPlan(contractId, contractType);
					this.saveNotPaymentPlan(newPaymentPlan1, fund, contractId, scheduleDate, scheduleSum);
				}
			map.put("sum", sums);
		}
		map.put("code",1);
		return map;
}

	/**
	 *  财务人员开票历史数据
	 * @param start
	 * @param length
	 * @param contractId
	 * @param contractType
	 * @param o
	 * @param o1
	 * @return
	 */
	@Override
	public List<PaymentRegist> getPaymentAllHistory(int start, int length, String contractId, String contractType, Object o, Object o1) {
		Page<PaymentRegist> paymentRegists=null;
		Pageable pageable=new PageRequest(start/length,length, Sort.Direction.DESC,"createTime");
		paymentRegists=paymentRegistRepository.findByContractIdAndContractType(contractId,contractType,pageable);
		return paymentRegists.getContent();
	}
	/**
	 * 财务-开票数据条数
	 * @param contractId
	 * @return
	 */
	@Override
	public Long getPaymentAllRegistCount(String contractId) {
		Long count=paymentRegistRepository.getPaymentRegistCount(contractId);
		return count;
	}
	/**
	 * 回款历史数据-财务
	 * @param start
	 * @param length
	 * @param contractId
	 * @param contractType
	 * @param o
	 * @param o1
	 * @return
	 */
	@Override
	public List<PayRegist> getPayAllHistory(int start, int length, String contractId, String contractType, Object o, Object o1) {
		Page<PayRegist> payRegists=null;
		Pageable pageable=new PageRequest(start/length,length, Sort.Direction.DESC,"createTime");
		payRegists=payRegistRepository.findByContractIdAndContractType(contractId,contractType,pageable);
		return payRegists.getContent();
	}
	/**
	 * 回款历史条数-财务
	 * @param contractId
	 * @return
	 */
	@Override
	public Long getPayAllRegistCount(String contractId) {
		Long count=payRegistRepository.getPayRegistCount(contractId);
		return count;
	}

	/**
	 * 修改计划更新回款计划表
	 * @param payRegist
	 * @param contractId
	 */
	@Override
	public void resetPayPlan(List<PayRegist> payRegist, String contractId,String contractType) {
		payPlanRepository.deleteBycontractId(contractId);
		List<PaymentInitial> paymentSchedules = paymentInitialRepository.findByContractIdAndContractType(contractId, contractType);
		String fund[] = new String[7];
		String planId[] = new String[7];
		float scheduleSum[] = new float[7];
		Date scheduleDate[] = new Date[7];
		for (int n = 0; n <= 6; n++) {
			fund[n] = paymentSchedules.get(n).getFund();
			planId[n] = paymentSchedules.get(n).getId();
			scheduleSum[n] = paymentSchedules.get(n).getAmount();
			scheduleDate[n] = paymentSchedules.get(n).getPayDate();
		}
		for (int j = 0; j < payRegist.size(); j++) {
			List<PayPlan> newPayPaln = null;
			newPayPaln = payPlanRepository.getNewPayPlan(contractId, contractType);
			float sum[] = new float[7];
			if (newPayPaln.size() > 0) {
				PayPlan newData = newPayPaln.get(0);
				String fundNew = newData.getFund();
				float scheduleSumNew = newData.getScheduleSum();
				float paidStage = newData.getPaidStage();
				int num = 0;
				for (int n = 0; n <= 6; n++) {
					fund[n] = paymentSchedules.get(n).getFund();
					if (fund[n].equals(fundNew)) {
						num = n;
					}
				}
				if (paidStage == scheduleSumNew) {
					sum[num] = payRegist.get(j).getPaidSum();
					for (int i = num + 1; i <= 6; ) {
						if (scheduleSum[i] >= sum[i - 1]) {
							this.savePayPlan(fund[i], planId[i], scheduleSum[i], sum[i - 1], scheduleDate[i], payRegist.get(j));
							break;
						} else {
							this.savePayPlan(fund[i], planId[i], scheduleSum[i], scheduleSum[i], scheduleDate[i], payRegist.get(j));
							sum[i] = sum[i - 1] - scheduleSum[i];
							i++;
							try {
								Thread.sleep(5);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						}
					}
				} else {
					sum[num] = scheduleSumNew - paidStage;
					if (sum[num] >= payRegist.get(j).getPaidSum()) {
						this.savePayPlan(fund[num], planId[num], sum[num], payRegist.get(j).getPaidSum(), scheduleDate[num], payRegist.get(j));
					} else {
						this.savePayPlan(fund[num], planId[num], sum[num], sum[num], scheduleDate[num], payRegist.get(j));
						sum[num + 1] = payRegist.get(j).getPaidSum() - sum[num];
						try {
							Thread.sleep(5);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
						for (int i = num + 1; i <= 6; ) {
							if (scheduleSum[i] >= sum[i]) {
								this.savePayPlan(fund[i], planId[i], scheduleSum[i], sum[i], scheduleDate[i], payRegist.get(j));
								break;
							} else {
								this.savePayPlan(fund[i], planId[i], scheduleSum[i], scheduleSum[i], scheduleDate[i], payRegist.get(j));
								i++;
								sum[i] = sum[i - 1] - scheduleSum[i - 1];
								try {
									Thread.sleep(5);
								} catch (InterruptedException e) {
									e.printStackTrace();
								}
							}
						}
					}
				}
			} else {
				sum[0] = payRegist.get(j).getPaidSum();
				for (int i = 0; i <= 6; ) {
					if (scheduleSum[i] >= sum[i]) {
						this.savePayPlan(fund[i], planId[i], scheduleSum[i], sum[i], scheduleDate[i], payRegist.get(j));
						break;
					} else {
						this.savePayPlan(fund[i], planId[i], scheduleSum[i], scheduleSum[i], scheduleDate[i], payRegist.get(j));
						i++;
						sum[i] = sum[i - 1] - scheduleSum[i - 1];
						try {
							Thread.sleep(5);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
				}
			}
		}
		List<PayPlan> newPayPlan1 = null;
		newPayPlan1 = payPlanRepository.getNewPayPlan(contractId, contractType);
		this.saveNotPayPlan(newPayPlan1, fund, contractId, scheduleDate, scheduleSum);
	}
	/**
	 * 修改计划更新开票计划表
	 * @param paymentRegist
	 * @param contractId
	 */
	@Override
	public void resetPaymentPlan(List<PaymentRegist> paymentRegist, String contractId, String contractType) {
		paymentPlanRepository.deleteBycontractId(contractId);
		List<PaymentInitial> paymentSchedules = paymentInitialRepository.findByContractIdAndContractType(contractId, contractType);
		String fund[] = new String[7];
		String planId[] = new String[7];
		float scheduleSum[] = new float[7];
		Date scheduleDate[] = new Date[7];
		for (int n = 0; n <= 6; n++) {
			fund[n] = paymentSchedules.get(n).getFund();
			planId[n] = paymentSchedules.get(n).getId();
			scheduleSum[n] = paymentSchedules.get(n).getAmount();
			scheduleDate[n] = paymentSchedules.get(n).getPayDate();
		}
		for (int j = 0; j < paymentRegist.size(); j++) {
			List<PaymentPlan> newPaymentPaln = null;
			newPaymentPaln = paymentPlanRepository.getNewPaymentPlan(contractId, contractType);
			float sum[] = new float[7];
			if (newPaymentPaln.size() > 0) {
				PaymentPlan newData = newPaymentPaln.get(0);
				String fundNew = newData.getFund();
				float scheduleSumNew = newData.getScheduleSum();
				float paidStage = newData.getPaymentStage();
				int num = 0;
				for (int n = 0; n <= 6; n++) {
					fund[n] = paymentSchedules.get(n).getFund();
					if (fund[n].equals(fundNew)) {
						num = n;
					}
				}
				if (paidStage == scheduleSumNew) {
					sum[num] = paymentRegist.get(j).getPaymentSum();
					for (int i = num + 1; i <= 6; ) {
						if (scheduleSum[i] >= sum[i - 1]) {
							this.savePaymentPlan(fund[i], planId[i], scheduleSum[i], sum[i - 1], scheduleDate[i], paymentRegist.get(j));
							break;
						} else {
							this.savePaymentPlan(fund[i], planId[i], scheduleSum[i], scheduleSum[i], scheduleDate[i], paymentRegist.get(j));
							sum[i] = sum[i - 1] - scheduleSum[i];
							i++;
							try {
								Thread.sleep(5);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						}
					}
				} else {
					sum[num] = scheduleSumNew - paidStage;
					if (sum[num] >= paymentRegist.get(j).getPaymentSum()) {
						this.savePaymentPlan(fund[num], planId[num], sum[num], paymentRegist.get(j).getPaymentSum(), scheduleDate[num], paymentRegist.get(j));
					} else {
						this.savePaymentPlan(fund[num], planId[num], sum[num], sum[num], scheduleDate[num], paymentRegist.get(j));
						sum[num + 1] = paymentRegist.get(j).getPaymentSum() - sum[num];
						try {
							Thread.sleep(5);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
						for (int i = num + 1; i <= 6; ) {
							if (scheduleSum[i] >= sum[i]) {
								this.savePaymentPlan(fund[i], planId[i], scheduleSum[i], sum[i], scheduleDate[i], paymentRegist.get(j));
								break;
							} else {
								this.savePaymentPlan(fund[i], planId[i], scheduleSum[i], scheduleSum[i], scheduleDate[i], paymentRegist.get(j));
								i++;
								sum[i] = sum[i - 1] - scheduleSum[i - 1];
								try {
									Thread.sleep(5);
								} catch (InterruptedException e) {
									e.printStackTrace();
								}
							}
						}
					}
				}
			} else {
				sum[0] = paymentRegist.get(j).getPaymentSum();
				for (int i = 0; i <= 6; ) {
					if (scheduleSum[i] >= sum[i]) {
						this.savePaymentPlan(fund[i], planId[i], scheduleSum[i], sum[i], scheduleDate[i], paymentRegist.get(j));
						break;
					} else {
						this.savePaymentPlan(fund[i], planId[i], scheduleSum[i], scheduleSum[i], scheduleDate[i], paymentRegist.get(j));
						i++;
						sum[i] = sum[i - 1] - scheduleSum[i - 1];
						try {
							Thread.sleep(5);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
				}
			}
		}
		List<PaymentPlan> newPaymentPlan1 = null;
		newPaymentPlan1 = paymentPlanRepository.getNewPaymentPlan(contractId, contractType);
		this.saveNotPaymentPlan(newPaymentPlan1, fund, contractId, scheduleDate, scheduleSum);
	}


	
}
